package myhttp

import (
	"fmt"
	tools "go-lang-tools"
	"net/http"
	"net/http/httputil"
)

func RequestDeomo() {
	resp, err := http.Get("https://www.wsmtec.com")
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	s, err2 := httputil.DumpResponse(resp, true)
	if err2 != nil {
		panic(err2)
	}
	fmt.Printf("%s", s)
}

func RequestDeomo2() {
	request, err := http.NewRequest(http.MethodGet, "http://www.imooc.com", nil)
	if err != nil {
		panic(err)
	}
	request.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")
	resp, err := http.DefaultClient.Do(request)
	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	reqInfo, err := httputil.DumpRequest(request, false)
	if err != nil {
		panic(err)
	}
	fmt.Println("resqInfo:")
	fmt.Println(string(reqInfo))
	s, err2 := httputil.DumpResponse(resp, true)
	if err2 != nil {
		panic(err2)
	}
	fmt.Println("respInfo:")
	fmt.Println(string(s))
}

func RequestDeomo3() {
	request, err := http.NewRequest(http.MethodGet, "http://www.imooc.com", nil)
	if err != nil {
		panic(err)
	}
	request.Header.Add("User-Agent", "Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1")

	client := http.Client{
		CheckRedirect: func(req *http.Request, via []*http.Request) error {
			fmt.Println("Redirect:", req)
			return nil
		},
	}
	resp, err := client.Do(request)

	if err != nil {
		panic(err)
	}
	defer resp.Body.Close()

	reqInfo, err := httputil.DumpRequest(request, false)
	if err != nil {
		panic(err)
	}
	fmt.Println("resqInfo:")
	fmt.Println(string(reqInfo))
	s, err2 := httputil.DumpResponse(resp, true)
	if err2 != nil {
		panic(err2)
	}
	fmt.Println("respInfo:")
	fmt.Println(string(s))
}

func RequestDeomo4() {
	tools.Logger = tools.WriteLogFile("./log", "")
	//resp, _ := tools.SendPostRequstJsonWithCookies2(`https://www.jisilu.cn/data/cbnew/cb_list_new/?___jsl=LST___t=1647854703401`, "",
	resp, _ := tools.SendPostRequstJsonWithCookies2(`https://www.jisilu.cn/data/bond_ajax/cb_yield/?bond_id=127043&is_cb=true&bond_price=144.572`, "",
		[]string{
			"kbzw__user_login", "7Obd08_P1ebax9aX7sff2Nzg1NrEoZGtmrCW6c3q1e3Q6dvR1YyilKWyp6CxoqmWrJTaqampkdXFpNuozaydqJKtmaur15mcndbd3dPGpJ2tmKaVqq-Us47FotLWoLbo5uDO4sKmrKGogZi43efZ2PDfl7DKgainoaickLjd56udtIzvmKqKl7jj6M3VuNnbwNLtm6yVrY-qrZOgrLi1wcWhieXV4seWqNza3ueKkKTc6-TW3puwl6SRpaupq5mel62ZyMrfzenLpZaqrqGrlw..", "www.jisilu.cn",
			"kbzw__Session", "8t1sm3m19oa1d0ugf6bkrb3rn0", "www.jisilu.cn",
		}...)
	fmt.Println(resp)

	data, _ := tools.JsonToMap(resp)
	tools.OutPutInfo(nil, data["result"].(map[string]interface{})["ytm_rt"])

}
